package com.hsj.reader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hsj.reader.entity.Evaluation;
import com.hsj.reader.entity.Member;
import com.hsj.reader.entity.MemberReadState;
import com.hsj.reader.mapper.EvaluationMapper;
import com.hsj.reader.mapper.MemberMapper;
import com.hsj.reader.mapper.MemberReadStateMapper;
import com.hsj.reader.service.MemberService;
import com.hsj.reader.service.exception.BussinessException;
import com.hsj.reader.utils.MD5Utils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.*;

@Service("memberService")
@Transactional
public class MemberServiceImpl implements MemberService {

    @Resource
    private MemberMapper memberMapper;
    @Resource
    private MemberReadStateMapper memberReadStateMapper;

    @Resource
   private EvaluationMapper evaluationMapper;
    @Override
    public Member createMember(String username, String password, String nickname) {

        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);//查询数据库中现有的username
        List<Member> memberList =memberMapper.selectList(queryWrapper);

        if(memberList.size()>0){

        throw new BussinessException("M01","用户已存在");
        }
    //-----  判断 新增的 user 是否存在 不存在才新增
        Member member = new Member();
        member.setUsername(username);
        member.setNickname(nickname);

        int salt = new Random().nextInt(1000)+1000;

        String md5 = MD5Utils.md5Digest(password,salt);
        member.setPassword(md5);
        member.setSalt(salt);
        member.setCreateTime(new Date());
        memberMapper.insert(member);
        return member;

    }

    @Override
    public Member checkLogin(String username, String password) {

        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);//查询数据库中现有的username
       Member member = memberMapper.selectOne(queryWrapper);

       //对用户进行校验
        if(member == null){
            throw new BussinessException("M02","用户不存在");
        }
        //对密码进行校验 密码 与 member 的salt 结合 生成 新的 md5 摘要 与 数据库里的比较
        String md5 = MD5Utils.md5Digest(password,member.getSalt());
        if (!md5.equals(member.getPassword())){
            throw new BussinessException("M03","输入密码错误");
        }
        return member;

    }

    @Override
    public MemberReadState selectMemberReadState(Long memberId, Long bookId) {
        QueryWrapper<MemberReadState> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("book_id",bookId);
        queryWrapper.eq("member_id",memberId);
        MemberReadState memberReadState = memberReadStateMapper.selectOne(queryWrapper);
        return memberReadState;
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)
    @Override
    public MemberReadState updateMemberReadState(Long memberId, Long bookId, Integer readState) {
            QueryWrapper<MemberReadState> queryWrapper = new QueryWrapper<MemberReadState>();
            queryWrapper.eq("book_id",bookId);
            queryWrapper.eq("member_id",memberId);
            MemberReadState memberReadState = memberReadStateMapper.selectOne(queryWrapper);

            if(memberReadState == null){
                memberReadState = new MemberReadState();
                memberReadState.setMemberId(memberId);
                memberReadState.setBookId(bookId);
                memberReadState.setReadState(readState);
            }else{
                memberReadState.setReadState(readState);
                memberReadStateMapper.updateById(memberReadState);



            }


        return memberReadState;
    }

    @Override
    public Evaluation evaluate(Long memberId, Long bookId, Integer score, String content) {
        Evaluation evaluation = new Evaluation();
        evaluation.setMemberId(memberId);
        evaluation.setBookId(bookId);
        evaluation.setScore(score);
        evaluation.setContent(content);
        evaluation.setCreateTime(new Date());
        evaluation.setState("enable");
        evaluation.setEnjoy(0);
        evaluationMapper.insert(evaluation);

        return evaluation;




    }
    @Override
    public Evaluation enjoy(Long evaluationId) {
        Evaluation evaluation = evaluationMapper.selectById(evaluationId);
        evaluation.setEnjoy(evaluation.getEnjoy()+1);
        evaluationMapper.updateById(evaluation);
        return evaluation;
    }


}





